<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class UploadForm extends CFormModel
{
	public $itemName;
	public $cmisId;
	public $itemType;
	public $fileName;
	private $_identity;
	public $infoTypes = array('topic'=>"Topic",'concept'=>"Concept","task"=>"Task","reference"=>"Reference","map"=>"DITA Map");

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('itemName,cmisId', 'required'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'itemName'=>'Name',
			'itemType'=>'Information Type',
		);
	}

	public function getFileName() {
		return preg_replace("/[a-z0-9]*/","",strtolower($this->itemName));
	}

	public function handleAction($atomPubTemplate, $cmisId, $objType = false, $fileName = false, $ditaType = false,$topicTitle = false) {
		try {
                $url = "http://admin:admin@localhost:8080/alfresco/s/cmis" . $cmisId;
                $ch = curl_init();
                $file = file_get_contents("protected/models/atom/" . $atomPubTemplate . ".atom");
                $file = str_replace("%type%",$objType,$file);
                $file = str_replace("%title%",$fileName,$file);
                $file = str_replace("%content_type%","application/ms-word",$file);
                $file = str_replace("%summary%",$topicTitle,$file);
                $putData = tmpfile();
                fwrite($putData, $file);
                fseek($putData, 0);
                curl_setopt($ch,CURLOPT_URL,$url);
		switch($atomPubTemplate) {
			case "delete":
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'DELETE /alfresco/s/cmis' . $cmisId . " HTTP/1.1\r\n");
				break;
			case "create":
				if ($ditaType) {
				$content = file_get_contents("protected/models/dita/" . strtolower($ditaType) . ".xml");
				$content = str_replace("<?xm-replace_text Title?>",$topicTitle,$content);
				$content = str_replace("%id%",md5(time()),$content);
                		$file = str_replace("%content%",base64_encode($content),$file);
				}
				$method = CURLOPT_POST;
                		curl_setopt($ch,$method,1);
                		curl_setopt($ch,CURLOPT_POSTFIELDS,$file);
				break;
			case "update":
				$method = CURLOPT_PUT;
                		curl_setopt($ch,$method,1);
                		curl_setopt($ch, CURLOPT_INFILE, $putData);
                		curl_setopt($ch, CURLOPT_INFILESIZE, strlen($file));
				break;
		}

                //curl_setopt($ch, CURLOPT_INFILE, $putData);
                //curl_setopt($ch, CURLOPT_INFILESIZE, strlen($file));
                curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/atom+xml"));
                curl_setopt($ch,CURLOPT_VERBOSE,0);
                curl_setopt($ch,CURLOPT_URL,$url);
		ob_start();
                $result = @curl_exec($ch);
				$out = ob_get_contents();
                curl_close($ch);
				//echo $out;
		ob_end_clean();
		} catch (Exception $e) {
			return "Failed";
		}
		return "OK";
	}

	public function addFolder($name, $cmisId) {
		return $this->handleAction('create', $cmisId, 'folder',$name);
	}

	//objType document|folder
	//ditaType topic|concept|reference|task|map
	//$topicTitle topic title
	//$fileName filesystem name
	//cmisId id of CMIS object
	public function addFile($ditaType, $topicTitle, $fileName, $cmisId) {
		$this->handleAction('create',$cmisId, 'document', $fileName, $ditaType, $topicTitle);
		return "OK";
	}

	public function delete($cmisId) {
		return $this->handleAction('delete',$cmisId);
	}
}
